#include "init.h"
#include "comm/boot_info.h"
#include "cpu/cpu.h"
#include "cpu/irq.h"
#include "dev/time.h"
#include "tools/log.h"
#include "tools/klib.h"
#include "tools/list.h"
#include "os_cfg.h"
#include "core/task.h"
#include "comm/cpu_instr.h"
#include "ipc/sem.h" 
#include "core/memory.h"
#include "dev/console.h"
#include "dev/kbd.h"
#include "fs/fs.h"

const char *str = "abcdefg";

void kernel_init(boot_info_t *boot_info) {
    cpu_init();
    irq_init();
    log_init();
    
    log_printf("Memory initialization begins");
    memory_init(boot_info);
    log_printf("Memory initialization successful");

    log_printf("File system initialization begins");
    fs_init();
    log_printf("File system initialization successful");
    
    log_printf("Time interrupt initialization begins");
    time_init();
    log_printf("Time interrupt initialization successful");
    
    log_printf("Task initialization begins");
    task_manager_init();
    log_printf("Task initialization successful");
}
void move_to_first_task(void) {
    task_t *curr = task_current();
    ASSERT(curr != 0);

    tss_t *tss = &(curr->tss);
    __asm__ __volatile__(
        "push %[ss]\n\t"
        "push %[esp]\n\t"
        "push %[eflags]\n\t"
        "push %[cs]\n\t"
        "push %[eip]\n\t"
        "iret"::
        [ss]"r"(tss->ss), [esp]"r"(tss->esp),
        [eflags]"r"(tss->eflags), [cs]"r"(tss->cs), [eip]"r"(tss->eip)
    );
}

void init_main(void) {
    log_printf("kernel is running....");
    
    task_first_init();
    move_to_first_task();
} 